package ubadb.core.components.bufferManager.bufferPool.replacementStrategies.LRU;
import java.util.Collection;
import java.util.Date;

import ubadb.core.common.Page;
import ubadb.core.components.bufferManager.bufferPool.BufferFrame;
import ubadb.core.components.bufferManager.bufferPool.replacementStrategies.PageReplacementStrategy;
import ubadb.core.exceptions.PageReplacementStrategyException;

public class LRU implements PageReplacementStrategy {
public BufferFrame findVictim(Collection<BufferFrame> bufferFrames) throws PageReplacementStrategyException{
	LRUBufferFrame victim = null;
	Date olderF = null; //horrible pero bue

	for (BufferFrame bufferFrame : bufferFrames) {
		LRUBufferFrame LRUBF = (LRUBufferFrame) bufferFrame;		//mm si la casteo donde esta la fecha?
		// LRUBF = LRU BufferFrame
		if (LRUBF.canBeReplaced() && ((olderF == null) || LRUBF.getPDate().before(olderF))) {
			victim = LRUBF;
			olderF = LRUBF.getPDate();
		}
	}
	
	if (victim == null)
		throw new PageReplacementStrategyException("Failed to find Victim");
	return victim;
}

	
public BufferFrame createNewFrame(Page page) {
	return new LRUBufferFrame(page);
}
}
